package com.unionpay.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	public static final String DATETIME_FORMAT = "yyyyMMddHHmmss";
    public static final String DATE_FORMAT = "yyyyMMdd";
    public static final String DATE_SHORT_FORMAT = "yyMMdd";
    public static final String TIME_FORMAT = "HHmmss";
    public static final String DAY_FORMAT = "MMdd";

    public static String getDate(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    public static String getDateTime(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    public static String getShortDate(Date date){
    	return getDate(date,DATE_SHORT_FORMAT);
    }
    public static String getCurShortDate(){
    	return getShortDate(new Date(System.currentTimeMillis()));
    }
    
    public static String getDate(Date date) {
        return getDate(date, DATE_FORMAT);
    }
    public static String getCurDate(){
    	return getDate(new Date(System.currentTimeMillis()));
    }

    public static String getDateTime(Date date) {
        return getDateTime(date, DATETIME_FORMAT);
    }
    public static String getCurDateTime(){
    	return getDateTime(new Date(System.currentTimeMillis()));
    }

    public static String getHourMinuteOfTime(Date date) {
        return new SimpleDateFormat(TIME_FORMAT).format(date);
    }
    public static String getCurHourMinuteOfTime() {
    	return getHourMinuteOfTime(new Date(System.currentTimeMillis()));
    }
    
    public static String getDayOfMonth(Date date) {
        return new SimpleDateFormat(DAY_FORMAT).format(date);
    }
    public static String getCurDayOfMonth(){
    	return getDayOfMonth(new Date(System.currentTimeMillis()));
    }

    public static Date getDate(Date date, int monthInterval) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, monthInterval);
        return cal.getTime();
    }

    public static Date getDate(String time) {
        try {
            return new SimpleDateFormat(DATETIME_FORMAT).parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDate(String time, String format) {
        try {
            return new SimpleDateFormat(format).parse(time);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
		// TODO Auto-generated method stub

    	System.out.println(DateUtil.getCurDate());
    	
    	System.out.println(DateUtil.getCurShortDate());
    	
    	System.out.println(DateUtil.getCurHourMinuteOfTime());
    	
	}
    
}
